package org.itboys.kitchen.controller.income;


import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.entity.mongo.KitchenExpressMode;
import org.itboys.kitchen.entity.mysql.income.KitchenIncomeProduct;
import org.itboys.kitchen.service.mongo.KitchenExpressModeService;
import org.itboys.kitchen.service.mysql.income.KitchenIncomeProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 包名：com.hz.sunday.xlcl.controller.web
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2015年10月8日 下午3:47:21 
 * 描述:农厨_钱包（收益）_controller
 *
 */
@Controller
@RequestMapping("/kitchen/web/income")
public class KitchenIncomeController extends BaseController{
	@Autowired 
	private KitchenIncomeProductService incomeProductService;


	/**
	 * 商品主页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/product/index")
	public String product_index(
			HttpServletRequest request,
			Model model){
		return "/kitchen/income/product/index";
	}

	/**
	 * 运费模板查询。分页
	 * @param request
	 * @param response
	 */
	@RequestMapping("/product/select")
	public void product_select(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			
			PageResult<KitchenIncomeProduct> page = incomeProductService.select(request);
			JsonPageUtils.renderJsonPage(page.getTotal(),page.getData(),response);
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/kitchen/web/income/product/select------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
		}

	}

	/**
	 *运费模板查询。单个
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping("/product/findOne")
	public void product_findOne(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="id",required=true)Long id){
		try {
			KitchenIncomeProduct result = incomeProductService.find(id);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result,CommonConstants.SUCCESS));
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/kitchen/web/income/product/findOne------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
		}
	}
	/**
	 *新增或编辑
	 * @param request
	 * @param response
	 * @param incomeProduct
	 */
	@RequestMapping("/product/save")
	public void product_save(
			HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute KitchenIncomeProduct incomeProduct ){
		try {
			incomeProductService.saveIncomeProduct(incomeProduct);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE,CommonConstants.SUCCESS));
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/kitchen/web/income/product/save------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
		}
	}

	/**
	 * 上下架
	 * @param request
	 * @param response
	 * @param id
	 * @param status
	 */
	@RequestMapping("/product/change")
	public void product_change(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="id",required=true)Long id,
			@RequestParam(value="status",required=true)Integer status
			){
		try {
			incomeProductService.change(id,status);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE,CommonConstants.SUCCESS));
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/kitchen/web/income/product/change------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
		}
	}
}
